const mongoose = require('mongoose');
const shopModel = mongoose.model('shopModel');
const goodsModel = mongoose.model('goodsModel');
async function getproduct({ searchoose, select, pagesize, currentPage }) {
   //1. 获取当前集合的数据总条数
  // console.log(searchoose, select, pagesize, currentPage);
   const totals = await shopModel.countDocuments();
   if(select&&searchoose){
     // console.log('进入查询条件',searchoose, select, pagesize, currentPage);
     const data= await shopModel.find({[select]:{$regex:searchoose,$options:"$i"}})
      .limit(pagesize - 0)
      .skip((currentPage - 1) * pagesize)
      return { data, totals }
   }else{
      const data = await shopModel.find()
      .limit(pagesize - 0)
      .skip((currentPage - 1) * pagesize)

     // console.log('第三层渲染查询',data);
     return { data, totals }
   }
   
}
//增加库存
async function addproduct(datas) {
   const data = await shopModel.create(datas)
   return data
}
//查询商品
async function search(searchoose, select) {
   const data = await shopModel.find({
      //精确查询
      [searchoose]: { $regex: searchoose, $options: "si" }
   })
   console.info("33333", data)
   return data
}

//删除商品
async function renoveone(datas) {
   const data = await shopModel.deleteOne(datas)
   return data
}
//修改商品，获取ID
async function getupdataproductID({ _id }) {
  // console.log('第三层',{_id});
   const data = await shopModel.find(_id)
   return data
}
//修改商品
async function updataproduct({ _id, goodsname, types, orgin, desc, price, guige, shop, num }) {
   const data = await shopModel.updateOne({ _id }, { goodsname, types, orgin, desc, price, guige, shop, num })
   return data
}


//获取商品上传列表数据
async function getgoods() {
   const data = await goodsModel.find()
   return data
}
async function show(datas) {
  // console.info("2333333333",datas)
   const data = await goodsModel.create(datas)
   //console.info("2333333333",data)

   return data
}
module.exports = {
   getproduct, addproduct, renoveone, getupdataproductID, updataproduct, search, getgoods,show
}